package com.efounder.view;

import android.content.Context;
import androidx.appcompat.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;

import com.efounder.chat.widget.BadgeView;
import com.efounder.interfaces.Ibadge;
import com.efounder.mobilecomps.R;


/**
 * Created by lch on 2017/6/5.
 */

public class EFTextView extends AppCompatTextView implements Ibadge {
    private int chatId;
    private BadgeView badgeView;
    private Context context;
    private int badgeCount;

    public EFTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EFTextView(Context context) {
        this(context, null);
    }

    /**
     * 获得我自定义的样式属性
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public EFTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;

    }

    @Override
    public void setChatId(int chatId) {
        this.chatId = chatId;
    }

    @Override
    public void initBadgeView(BadgeView badgeView) {
        if (this.badgeView == null)
            this.badgeView = badgeView;
    }

    @Override
    public BadgeView getBadgeView() {
//        if (this.badgeView == null) {
//            badgeView = new BadgeView(context, this);
//            badgeView.setBadgeMargin(0, 0);
//            badgeView.setTextSize(13);
//        }
        return this.badgeView;
    }

    @Override
    public void setBadgeCount(int badgeCount) {
        this.badgeCount = badgeCount;
        if (badgeCount > 99) {
            badgeView.setText("99+");
        } else {
            badgeView.setText(badgeCount + "");
        }
        //ShortcutBadger.applyCount(context, badgeCount);
        badgeView.setBadgeBackgroundColor(getResources().getColor(
                R.color.badge_color));
        if (badgeCount <= 0 && badgeCount != -1) {
            badgeView.setVisibility(View.INVISIBLE);
        } else {
            if(badgeCount == -1){
                badgeView.setText("");
                badgeView.setBadgeMargin(0,5);
                badgeView.setTextSize(7);
            }else {
                badgeView.setBadgeMargin(0, 0);
                badgeView.setTextSize(11);
            }
            badgeView.show();
        }
    }

    @Override
    public int getBadgeCount() {
        return badgeCount;
    }
}
